//
//  JWSingleSite.m
//  JWBrowser
//
//  Created by Dejun Liu on 14-11-11.
//  Copyright (c) 2014年 JW. All rights reserved.
//

#import "JWSingleSite.h"
#import "AFNetworking.h"
#import "AFURLResponseSerialization.h"

@implementation JWSingleSite

+ (instancetype)sharedInstance
{
    static id instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _manager = [AFHTTPRequestOperationManager manager];
        AFHTTPResponseSerializer *serializer = [AFHTTPResponseSerializer serializer];
        serializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/javascript",@"baiduapp/json",@"text/html",nil];
        _manager.responseSerializer = serializer;
    }
    return self;
}

- (void)GetURLString:(NSString *)url params:(NSDictionary *)params jsonResponse:(Complation ) complation
{
    [self.manager GET:[url URLEncodeString] parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        complation(responseObject,nil);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        complation(nil,error);
    }];
}

- (void)PostURLString:(NSString *)url params:(NSDictionary *)prarams jsonResponse:(Complation) complation
{
    [self.manager POST:[url URLEncodeString] parameters:prarams success:^(AFHTTPRequestOperation *operation, id responseData) {
        complation(responseData,nil);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        complation(nil,error);
    }];
}

@end
